﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class mobile_ajaxdata_Default : BaseDataPage
{
    protected override void Page_Load(object sender, EventArgs e)
    {
        string aciton = Request.Params["aciton"];
        switch (aciton)
        {
            case "GetSaleByCompRpt":
                GetSaleByCompRpt();
                break;
            case "GetSaleByShopRpt":
                GetSaleByShopRpt();
                break;
            case "GetStockList":
                GetStockList();
                break;
            case "GetShopList":
                GetShopList();
                break;
            case "CheckRights":
                CheckRights();
                break;
            case "KM_DailyPayAmt_rpt":
                KM_DailyPayAmt_rpt();
                break;
            case "KM_ShopSaleMoneyDtl_MbRpt":
                KM_ShopSaleMoneyDtl_MbRpt();
                break;
            case "LoadData":
                LoadData();
                break;
            case "shoptvlist":
                shoptvlist();
                break;
            case "shopincount":
                shopincount();
                break;
        }
    }

    private void shopincount()
    {
        string sql = @"select count(*)as shopcount from Bas_ShopEnTrans where isnull(finished,0) <> 1";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopcount\":\"" + dr["shopcount"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void shoptvlist()
    {
        int i = 0;
        string sql = @"select * from Bas_ShopEnTrans where isnull(finished,0) <> 1 order by startdate ";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + (++i).ToString()
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"shoparea\":\"" + dr["shoparea"]
                 + "\",\"startdate\":\"" + (Convert.ToDateTime(dr["startdate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["startdate"]).ToString("MM月dd日"))
                 + "\",\"enddate\":\"" + (Convert.ToDateTime(dr["enddate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["enddate"]).ToString("MM月dd日HH:mm"))
                 + "\",\"remark\":\"" + dr["remark"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void KM_ShopSaleMoneyDtl_MbRpt()
    {
        string userid =Request.Params["userid"];
        string sdate = Request.Params["sdate"];
        string edate = Request.Params["edate"];
        string area = Request.Params["area"];
        string bigarea = Request.Params["bigarea"];
        string matkind = Request.Params["matkind"];
        string shopid = Request.Params["shopid"];
        if (shopid == "")
        {
            shopid = GetUserShopDataRights(userid);
            if (shopid == "")
                shopid = "没有任何权限";
        }
        string sql = "KM_ShopSaleMoneyDtl_MbRpt '" + sdate + "','" + edate + "','" + SplitStr(area) + "','" +
            SplitStr(bigarea) + "','" + SplitStr(shopid) + "','" + SplitStr(matkind) + "'";
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + dr["orderid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"saleamt\":\"" + dr["saleamt"]
                 + "\",\"saleqty\":\"" + dr["saleqty"]
                 + "\",\"salecount\":\"" + dr["salecount"]
                 + "\",\"kdj\":\"" + dr["kdj"]
                 + "\",\"ldv\":\"" + dr["ldv"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private string GetUserShopDataRights(string userid)
    {
        string reslt = "";
        string sql = @"select shopid as id,shopname as name from [192.168.10.11].hkerp.dbo.bas_shop where stockid in(
                        select sourceid from sys_userdatarights 
                        where stype = 0 and userid=" + userid + ") order by shopname";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                reslt += dr["id"].ToString()+",";
            }
        }
        return reslt.TrimEnd(',');
    }

    private void LoadData()
    {
        string actname = Request.Params["actname"];
        string userid = Request.Params["userid"];
        string sql = "";
        switch (actname)
        {
            case "LoadArea":
                sql=@"select OperationAreaID as id,OperationAreaName as name from Bas_OperationArea";
                break;
            case "LoadBigArea":
                sql = @"select AreaID as id,AreaName as name from Bas_Area where parentid='' order by areaname";
                break;
            case "LoadMatkind":
                sql = @"select kindid as id,kindname as name from sd_mat_kind";
                break;
        }
        DataSet ds;
        if (actname == "LoadShop")
        {
            sql = @"select shopid as id,shopname as name from [192.168.10.11].hkerp.dbo.bas_shop where stockid in(
                        select sourceid from sys_userdatarights 
                        where stype = 0 and userid=" + userid + ") order by shopname";
            ds = mydb.DM_GetDateSet(sql);
        }
        else
        {
            ds = db.DM_GetDateSet(sql);
        }
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"id\":\"" + dr["id"]
                 + "\",\"name\":\"" + dr["name"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetShopList()
    {
        string userid = Request.Params["userid"];
        string sql = @"select * from [192.168.10.11].hkerp.dbo.bas_shop where stockid in(select sourceid from sys_userdatarights 
                        where stype = 0 and userid=" + userid + ") order by shopname";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void KM_DailyPayAmt_rpt()
    {
        string sdate = Request.Params["sdate"];
        string shopid = Request.Params["shopid"];
        if (shopid == "0")
        {
            shopid = "";
        }
        else
        {
            shopid = "(''" + shopid + "'')";
        }
        string sql = "KM_DailyPayAmt_rpt '" + sdate + "','" + shopid + "','orderid'";
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + dr["orderid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"payamt\":\"" + dr["payamt"]
                 + "\",\"upayamt\":\"" + dr["upayamt"]
                 + "\",\"kdjrate\":\"" + dr["kdjrate"]
                 + "\",\"kdj\":\"" + dr["kdj"]
                 + "\",\"allpayamt\":\"" + dr["allpayamt"]
                 + "\",\"targetmoney\":\"" + dr["targetmoney"]
                 + "\",\"wcl\":\"" + dr["wcl"]
                 + "\",\"mrate\":\"" + dr["mrate"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetSaleByShopRpt()
    {
        string stockid = Request.Params["stockid"];
        string barcode = Request.Params["barcode"];
        DataSet ds = db.DM_GetDateSet("exec KM_SaleByShopRpt '"+stockid+"','"+barcode+"'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"materialcode\":\"" + dr["materialcode"]
                 + "\",\"materialname\":\"" + dr["materialname"]
                 + "\",\"indate\":\"" + dr["indate"]
                 + "\",\"purqty\":\"" + dr["purqty"]
                 + "\",\"salqty\":\"" + dr["salqty"]
                 + "\",\"actualqty\":\"" + dr["actualqty"]
                 + "\",\"zkl\":\"" + dr["zkl"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetStockList()
    {
        string userid = Request.Params["userid"];
        string sql = @"select * from sys_userdatarights where stype = 0 and userid=" + userid + " order by sourcename";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"stockid\":\"" + dr["sourceid"]
                 + "\",\"stockname\":\"" + dr["sourcename"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    //检查浏览权限
    private void CheckRights()
    {
        string modecode = Request.Params["modecode"];
        string userid = Request.Params["userid"];
        string isBrowse = mydb.DM_GetExecuteScalarObj(@"select M_Browse from sys_modulerights a inner join sys_modules b on b.mid = a.mid
        where modulecode='" + modecode
            + "' and userid=" + userid).ToString();
        if (string.IsNullOrEmpty(isBrowse) || isBrowse =="-1")
        {
            Response.Write("NO_RIGHT");
            Response.End();
        }
        else
        {
            if (isBrowse.ToLower() == "true")
            {
                Response.Write("OK");
                Response.End();
            }
            else
            {
                Response.Write("NO_RIGHT");
                Response.End();
            }
        }
    }

    private void GetSaleByCompRpt()
    {
        string barcode = Request.Params["barcode"];
        DataSet ds = db.DM_GetDateSet("exec km_salebycomprpt'','" + barcode + "'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"materialcode\":\"" + dr["materialcode"]
                 + "\",\"materialname\":\"" + dr["materialname"]
                 + "\",\"saldate\":\"" + dr["saldate"]
                 + "\",\"dcount\":\"" + dr["dcount"]
                 + "\",\"purqty\":\"" + dr["purqty"]
                 + "\",\"salqty\":\"" + dr["salqty"]
                 + "\",\"actualqty\":\"" + dr["actualqty"]
                 + "\",\"scount\":\"" + dr["scount"]
                 + "\",\"xxl\":\"" + dr["xxl"]
                 + "\",\"zkl\":\"" + dr["zkl"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
}